fn build_lib(base_dir: &str) {
    let mut builder = cc::Build::new();
    builder.include(base_dir);
    for sf in vec![
        "font8.cpp",
        "font12.cpp",
        // "font12CN.c",
        "font16.cpp",
        "font20.cpp",
        "font24.cpp",
        // "font24CN.c",
        "GUI_Paint.cpp",
    ] {
        builder.file(format!("{}/{}", base_dir, sf));
    }
    builder.compile("gui");
}

#[cfg(feature = "build-ffi")]
fn binding(base_dir: &str) {
    use std::path::PathBuf;
    let bindings = bindgen::Builder::default()
        .header(format!("{}/GUI_Paint.h", base_dir))
        .clang_arg("-ffunction-sections")
        .clang_arg("-fdata-sections")
        .layout_tests(false)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks))
        .generate()
        .expect("Unable to generate bindings");

    let out = PathBuf::from(format!("{}/src/ffi.rs", env!("CARGO_MANIFEST_DIR")));
    bindings
        .write_to_file(out.as_path())
        .expect("Couldn't write bindings!");
}

fn main() {
    let base_dir = format!("{}/c", env!("CARGO_MANIFEST_DIR"));
    build_lib(base_dir.as_str());
    #[cfg(feature = "build-ffi")]
    binding(base_dir.as_str());
}
